package com.zlf.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import com.zlf.po.CommonResult;
import com.zlf.po.Food;
import com.zlf.service.FoodService;


@RestController
@RefreshScope  //开启动态刷新
@RequestMapping("/FoodController")
public class FoodController {

    @Autowired
    private FoodService foodService;

    @GetMapping("/listFoodByBusinessId/{businessId}")
    public CommonResult<List> listFoodByBusinessId(
            @PathVariable("businessId") Integer businessId){
        List<Food> list = foodService.listFoodByBusinessId(businessId);
        return new CommonResult(200,"success(10200)",list);
    }
    @PostMapping("/addFood/{foodName}/{foodPrice}/{businessId}")
    public CommonResult addFood(
            @PathVariable("foodName") String foodName,
            @PathVariable("foodPrice") double foodPrice,
            @PathVariable("businessId") int businessId
    ){
        Food param=new Food();
        param.setFoodName(foodName);
        param.setFoodImg("" +
                "DhAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAF" +
                "iUAABYlAUlSJPAAAANYSURBVGhD7ZffK2xRFMfvX6PMFE1pJj8KMQopJbxMSnmlEFLkRx7" +
                "kSUoRj5ineSAPiCfkQR7kR4oHD36FlF8R8r191z177hhz7zhnn8lJ51u7M2ftObU+a" +
                "6+19t6/8APkQjhFLoRT5EI4RS6EU6QN8f7+jre3N+Pte6QNcXV1hdLSUrS1teHl5cWwfl3Pz8/" +
                "Y3NzE8PAw7u7uDKs5aUPs7+8jEAhgYGDAsCTW7e0tzs/Psbq6iunpaTQ3N6OwsBDp" +
                "6elIS0uTMTQ0ZGlVtSEWFxfFgbm5OcPyV5eXlyguLo46GT9ycnJQX1+PcDiMk5MTSU0r0obgCmRm" +
                "ZmJiYgILCwvRsbOzg9PTU4EoKCjAzMyM2Le3t3F9fW1rHWlBMEWqq6sTRjkYDAoIIer" +
                "q6vD4+Gh8Zb+0IHZ3d5GVlYWuri5JHQ7a8vPz0dnZiYuLC+dDjIyMwOv1Yn193bAAKysr8Hg88lQ1" +
                "kQxCt01bhmBrLS8vR21tbbQ10pHW1laxcz5ZYceO+GCYkWUIFirb49TUlPyurKzE2toa2t" +
                "vbMTY2Jv9REHl5eTLPBtDS0oLu7u4Pg/WTm5uLo6Mj+c6sLEPQUUb87OxMOhNrg/UQq9h0Wlpags/n+xR" +
                "t1RxCoRAeHh4MqzlZhmAe39zcyO9kEE1NTdJuGfH4TVE1h2Sb5f+kVdhKySA6Ojo+1YtSou" +
                "ZgVimFiI+y6lzz8/Pynqg5WFFKIRhdRpnzlHK6oaEBT09PH5qDjlIKwfMU2ydXQIn/Jdjk5CRKSkrkBMzm" +
                "oKOUQKiNi2kUD0eH6TjhuAqzs7PGjHXZDnFwcIDGxkYcHx9La62oqIh2MYpdbXBwUCB02mqsbIX" +
                "o6emRvaCqqkpSKDs7W85Q6ojNZyQSQUZGhkDwyXpQ81ZlCwTThk5x8IbHcxLBmC6qE72+vmJ0dFRsRUVFs" +
                "vmVlZXZAqINwfsBI07n2PPprOpCak84PDxETU2NQNKmjhd7e3sCxG97e3txf38vdrPSgmAB9/f3SzSZJ" +
                "owmx/j4uDjMowkHneTgphfvKG906k7i9/uxvLxszHxd2ivBW9rW1pbx9keMPA+CnONKsNA3Njb+mTLcM5h+f" +
                "X19spJmZUtNfLdcCKfIhXCKXAinyIVwilwIZwj4DQkrGjqU3iA+AAAAAElFTkSuQmCC");
        param.setFoodExplain("新菜品");
        param.setFoodPrice(foodPrice);
        param.setBusinessId(businessId);
        int res=foodService.addFood(param);
        return new CommonResult(200,"success",res);
    }

    @PostMapping("/updateFood/{foodId}/{foodName}/{foodExplain}/{foodPrice}")
    public CommonResult updateFood(
            @PathVariable("foodId") int foodId,
            @PathVariable("foodName") String foodName,
            @PathVariable("foodExplain") String foodExplain,
            @PathVariable("foodPrice") double foodPrice
    ){
        Food param=new Food();
        param.setFoodId(foodId);
        param.setFoodName(foodName);
        param.setFoodExplain(foodExplain);
        param.setFoodPrice(foodPrice);
        int res=foodService.updateFood(param);
        return new CommonResult(200,"success",res);
    }

    @DeleteMapping("/deleteFood/{foodId}")
    public CommonResult deleteFood(@PathVariable Integer foodId) {
        try {
            int result = foodService.deleteFood(foodId);
            if (result > 0) {
                return new CommonResult(200, "删除成功", null);
            }
            return new CommonResult(500, "删除失败", null);
        } catch (Exception e) {
            return new CommonResult(500, e.getMessage(), null);
        }
    }

}